﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Configuration;
using GDoc.Domain;

namespace GDoc.Data.Sql
{
    public class StorageContext : DbContext
    {
        public StorageContext() : base(ConfigurationManager.ConnectionStrings["GDoc"].ConnectionString)
        {
            Database.SetInitializer<StorageContext>(new Initializer());
        }

        public class Initializer : DropCreateDatabaseIfModelChanges<StorageContext>
        {
//            public override void InitializeDatabase(StorageContext context)
//            {
//#if DEBUG
//                if (context.Database.Exists() && !context.Database.CompatibleWithModel(false))
//                {
//                    context.Database.Delete();
//                }
//#endif
//                if (!context.Database.Exists())
//                {
//                    context.Database.Create();
//                }
//            }

            protected override void Seed(StorageContext context)
            {
                // Add demo user
                var user = new User
                {
                    Email = "test@test.com",
                    Password = "test#1234",
                };

                var user2 = new User
                {
                    Email = "admin@test.com",
                    Password = "test#1234",
                };


                var user3 = new User
                {
                    Email = "member@test.com",
                    Password = "test#1234",
                };

                context.Users.Add(user);
                context.Users.Add(user2);
                context.Users.Add(user3);

                // Add demo account
                var account = new Account
                {
                    Name = "TEST"
                };

                context.Accounts.Add(account);

                var role = new UserRoleInAccount
                {
                    Account = account,
                    User = user,
                    Role = RoleEnum.Owner
                };

                var role2 = new UserRoleInAccount
                {
                    Account = account,
                    User = user2,
                    Role = RoleEnum.Administrator
                };

                var role3 = new UserRoleInAccount
                {
                    Account = account,
                    User = user3,
                    Role = RoleEnum.Member
                };

                account.Users.Add(role);
                account.Users.Add(role2);
                account.Users.Add(role3);

                var group1 = new UserGroup
                {
                    Name = "Administración",
                    Account = account,
                };

                var group2 = new UserGroup
                {
                    Name = "Ventas",
                    Account = account,
                };

                var group3 = new UserGroup
                {
                    Name = "IT",
                    Account = account,
                };

                group1.Users.Add(user);
                group1.Users.Add(user2);

                group2.Users.Add(user2);
                group2.Users.Add(user3);

                group3.Users.Add(user);
                group3.Users.Add(user3);

                account.Groups.Add(group1);
                account.Groups.Add(group2);
                account.Groups.Add(group3);

                context.SaveChanges();
            }
        }

        /// <summary>
        /// Entity Set for Accounts.
        /// </summary>
        public DbSet<Account> Accounts { get; set; }

        /// <summary>
        /// Entity Set for DocumentDefinitions.
        /// </summary>
        public DbSet<DocumentDefinition> DocumentDefinitions { get; set; }

        /// <summary>
        /// Entity Set for FieldDefinitions.
        /// </summary>
        public DbSet<FieldDefinition> FieldDefinitions { get; set; }

        /// <summary>
        /// Entity Set for Users.
        /// </summary>
        public DbSet<User> Users { get; set; }

        /// <summary>
        /// Entity Set for UserRolesInAccount.
        /// </summary>
        public DbSet<UserRoleInAccount> UserRolesInAccount { get; set; }

        /// <summary>
        /// Entity Set for UserGroups.
        /// </summary>
        public DbSet<UserGroup> UserGroups { get; set; }

    }
}
